package com.mk.service.platform;

import act.controller.Controller;
import cn.hutool.core.date.DateUtil;
import com.github.d925529.apidoc.ApiHttpMethod;
import com.github.d925529.apidoc.annotation.Api;
import com.github.d925529.apidoc.annotation.ApiMethod;
import com.github.d925529.apidoc.annotation.ApiParam;
import com.github.d925529.apidoc.annotation.ApiReturn;
import com.mk.domain.CmResource;
import com.mk.interceptor.ITranslate;
import com.mk.util.S2;
import org.osgl.mvc.annotation.*;
import org.osgl.util.S;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;


@Controller("cmResource")
@Api(group = "pl",title = "文件", path = "/cmResource")
@SuppressWarnings("unused")
public class CmResourceService extends BasePlatformService {
    @Inject
    private CmResource.Mapper cmResourceMapper;

    @With({ILogin.class, ITranslate.class})
    @PostAction("add.json")
    @ApiMethod(title = "增加保存资源库", path = "/add.json", method = ApiHttpMethod.POST)
    @ApiParam(name = "cmResource", title = "资源库", type = CmResource.class)
    @ApiReturn(description = "主键", type = String.class)
    public String add(@NotNull CmResource cmResource,String rsuris) {
        String id = S2.UUID();
        cmResource.setRsid(id);
        cmResource.setCreatetime(DateUtil.now());
        cmResource.setCreaterid(me.getUserid());
        if(S.isNotEmpty(rsuris)){
            cmResource.setRsuri(rsuris);
        }
        cmResourceMapper.insertTemplate(cmResource);
        return id;
    }


    @With({ILogin.class, ITranslate.class})
    @PutAction("update.json")
    @ApiMethod(title = "修改保存资源库", path = "/update.json", method = ApiHttpMethod.PUT)
    @ApiParam(name = "cmResource", title = "资源库", type = CmResource.class)
    @ApiReturn(description = "修改了的数量", type = int.class)
    public int update(@NotNull CmResource cmResource,String rsuris) {
        if(S.isNotEmpty(rsuris)){
            cmResource.setRsuri(rsuris);
        }
        return cmResourceMapper.updateTemplateById(cmResource);
    }

    @With(ILogin.class)
    @GetAction("view.json")
    @ApiMethod(title = "查询指定资源库", path = "/view.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "id", title = "资源库的主键", type = String.class)
    @ApiReturn(description = "资源库", type = CmResource.class)
    public CmResource view(@NotNull String id){
        return cmResourceMapper.single(id);
    }


    @With(ILogin.class)
    @DeleteAction("deleteByid.json")
    @ApiMethod(title = "删除指定资源", path = "/deleteByid.json", method = ApiHttpMethod.DELETE)
    @ApiParam(name = "rsid", title = "删除指定资源", type = String.class)
    @ApiReturn(description = "返回行数", type = int.class)
    public int deleteByid(@NotNull String rsid){
        return cmResourceMapper.deleteById(rsid);
    }
}
